package com.wula.gtsns.web.dwr;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.wula.gtsns.bo.AreasBO;
import com.wula.gtsns.bo.CitiesBO;
import com.wula.gtsns.bo.ProvincesBO;
import com.wula.gtsns.entity.Areas;
import com.wula.gtsns.entity.Cities;
import com.wula.gtsns.entity.Provinces;

public class Address_JS {

	public Address_JS() {
	}

	/**
	 * 加载省份列表为xml字符串
	 * 
	 * @return
	 */
	public String listProvinces() {
		ProvincesBO bo = new ProvincesBO();
		List<Provinces> list = bo.findAll();
		Document doc = null;
		try {
			doc = DocumentHelper.parseText("<root></root>");
		} catch (Exception e) {
			return "无法加载省份列表!";
		}
		Element context = doc.getRootElement().addElement("all");

		for (Provinces vo : list) {
			context.addElement("Province").addAttribute("id",
					vo.getProvinceid()).setText(vo.getProvince());
		}
		System.out.println(doc.asXML());
		return doc.asXML();
	}

	/**
	 * 根据省份id加载该省份的城市列表为xml字符串
	 * 
	 * @param id
	 * @return
	 */
	public String findCitiesByProvinceId(int id) {
		CitiesBO bo = new CitiesBO();
		List<Cities> list = bo.findByProvincesId(id);
		Document doc = null;
		try {
			doc = DocumentHelper.parseText("<root></root>");
		} catch (Exception e) {
			return "无法加载省份列表!";
		}
		Element context = doc.getRootElement().addElement("all");

		for (Cities vo : list) {
			context.addElement("Province").addAttribute("id", vo.getCityid())
					.setText(vo.getCity());
		}
		System.out.println(doc.asXML());
		return doc.asXML();
	}

	public String findAreaByCityId(int id) {
		AreasBO bo = new AreasBO();
		List<Areas> list = bo.findAreaByCityId(id);
		Document doc = null;
		try {
			doc = DocumentHelper.parseText("<root></root>");
		} catch (Exception e) {
			return "无法加载省份列表!";
		}
		Element context = doc.getRootElement().addElement("all");

		for (Areas vo : list) {
			context.addElement("Province").addAttribute("id", vo.getAreaid())
					.setText(vo.getArea());
		}
		System.out.println(doc.asXML());
		return doc.asXML();
	}
}
